//$('<script type="text/javascript" src="//localhost/ntts/crawler.js?t=' + (new Date).getTime() + '"></script>').appendTo('body');

/**
 * this is the object which handle all crawling request
 */
var count = 0;
var crawler = {
    /**
     *  create async request
     * @param method: (POST|GET|PUT|DELETE ...)
     * @url the: target url
     */
    createCORSRequest : function(method, url){
        var xhr = new XMLHttpRequest();
        if ("withCredentials" in xhr){
            // XHR has 'withCredentials' property only if it supports CORS
            xhr.open(method, url, true);
        } else if (typeof XDomainRequest != "undefined"){ // if IE use XDR
            xhr = new XDomainRequest();
            xhr.open(method, url);
        } else {
            xhr = null;
        }
        return xhr;
    },

    /**
     * method use to send an ajax request
     * @param method (POST|GET|PUT|DELETE ...)
     * @param url the target url
     * @param data the data that send to server (string or object)
     * @param callback function: will be called when finish the request
     */ 
    ajax : function(method, url, data, callback) {
	console.log(method, url, data);
        var request = crawler.createCORSRequest( method, url );
        if ( request ){
            // attach callback function
            if(typeof callback != 'undefined') {
                request.onload = callback;
            }

            // preapre data
            var params = [];
            if(typeof data != 'undefined') {
                if(typeof data == 'String' || typeof data == 'string') {
                    params = data;
                } else if (typeof data == 'Object' || typeof data == 'object') {
                    for(var i in data) {
                        params.push(i + '=' + data[i]);
                    }
                } else {
                    console.log('Ajax request does not support data type: ' + (typeof data));
                }
            }
            
			console.log(params);
			params = params.join('&');
            // Send request
			request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			request.setRequestHeader("Content-length", params.length);
			request.setRequestHeader("Connection", "close");
            request.send(params);
        }
    },

    // define url that use to save data
    saveCitiesUrl: '//localhost/ntts/saveCities.php',
    saveTrucksUrl: '//localhost/ntts/saveTrucks.php',

    // default states for US and CA
    usStates : [
         ["AL", "ALABAMA"], ["AK", "ALASKA"], ["AZ", "ARIZONA"], ["AR", "ARKANSAS"], ["CA", "CALIFORNIA"], ["CO", "COLORADO"], 
         ["CT", "CONNECTICUT"], ["DE", "DELAWARE"], ["DC", "DISTRICT OF COLUMBIA"], ["FL", "FLORIDA"], ["GA", "GEORGIA"], 
         ["HI", "HAWAII"], ["ID", "IDAHO"], ["IL", "ILLINOIS"], ["IN", "INDIANA"], ["IA", "IOWA"], ["KS", "KANSAS"], 
         ["KY", "KENTUCKY"], ["LA", "LOUISIANA"], ["ME", "MAINE"], ["MD", "MARYLAND"], ["MA", "MASSACHUSETTS"], 
         ["MI", "MICHIGAN"], ["MN", "MINNESOTA"], ["MS", "MISSISSIPPI"], ["MO", "MISSOURI"], ["MT", "MONTANA"], 
         ["NE", "NEBRASKA"], ["NV", "NEVADA"], ["NH", "NEW HAMPSHIRE"], ["NJ", "NEW JERSEY"], ["NM", "NEW MEXICO"], 
         ["NY", "NEW YORK"], ["NC", "NORTH CAROLINA"], ["ND", "NORTH DAKOTA"], ["OH", "OHIO"], ["OK", "OKLAHOMA"], 
         ["OR", "OREGON"], ["PA", "PENNSYLVANIA"], ["RI", "RHODE ISLAND"], ["SC", "SOUTH CAROLINA"], ["SD", "SOUTH DAKOTA"], 
         ["TN", "TENNESSEE"], ["TX", "TEXAS"], ["UT", "UTAH"], ["VT", "VERMONT"], ["VA", "VIRGINIA"], ["WA", "WASHINGTON"], 
         ["WV", "WEST VIRGINIA"], ["WI", "WISCONSIN"], ["WY", "WYOMING"]
     ],

     caStates : [
         ["AB", "ALBERTA"], ["BC", "BRITISH COLUMBIA"], ["MB", "MANITOBA"], ["NB", "NEW BRUNSWICK"], ["NF", "NEWFOUNDLAND"], 
         ["NS", "NOVA SCOTIA"], ["NT", "NORTHWEST TERRITORIES"], ["ON", "ONTARIO"], ["PE", "PRINCE EDWARD ISLAND"], 
         ["QC", "QUEBEC"], ["SK", "SASKATCHEWAN"], ["YK", "YUKON TERRITORY"]
     ],

     // get cities of a state
     getAllCities : function(countryCode){
		var stateId;
         if (typeof countryCode == 'undefined') {
			countryCode = 'us';
			stateId = crawler.usStates[count][0];
		 }
		 
         if(countryCode != 'us'){
			countryCode = 'can';
			stateId = crawler.caStates[count][0];
		 }else{
			countryCode = 'us';
			stateId = crawler.usStates[count][0];
		 }
		 console.log(countryCode,stateId);
         $.ajax({
             url: 'https://www.nttsbreakdown.com/ntts/programs/main/main_provider.php',
             cache: false,
             type : 'GET',
			 dataType: 'text',
             data: {
                 country: countryCode,
                 flag: 'load',
                 state_province : stateId,
                 type: 'city'
             },
             success: function(cities) {
                 crawler.saveAllCities(countryCode, stateId, cities);
				 count++;
				 if(countryCode != 'can'){
					if(count < crawler.usStates.length){
						crawler.getAllCities(countryCode);
					}
				 }else{
					if(count < crawler.caStates.length){
						crawler.getAllCities(countryCode);
					}
				 }
             }
         });
     },

     // save cities to database
     saveAllCities : function (countryCode, stateId, cities) {
         var params = {
             'countryCode' : countryCode,
             'stateId' : stateId,
             'cities' : cities
         };
         crawler.ajax('POST', crawler.saveCitiesUrl, params);
     },

     // crawl all cities and save to DB
     crawlAllCities : function(countryCode) {
         var stateData = countryCode == 'us' ? crawler.usSatates :  crawler.caStates;
         for(var i in stateDate) {
             crawler.getAllCities(countryCode, i);
         }
     },

     // get trucks
     getTrucks : function(countryCode, stateId, cityId, cateId){
         $.ajax({
             url: 'https://www.nttsbreakdown.com/ntts/programs/main/location_search_provider.php',
             cache: false,
             type : 'POST',
             data: {
                 start: 0,
                 limit: 100000,
                 category: 'category_6',
                 city: 'Alabaster',
                 state: 'AL',
                 country: countryCode,
                 dealers: '[]',
                 distance: '150',
                 flag: 'search',
                 page_host: 'www.nttsbreakdown.com',
                 page_path: '/ntts/programs/main/main.php',
                 page_protocol: 'https:',
                 page_url: 'https://www.nttsbreakdown.com/ntts/programs/main/main.php?vendor=1',
                 referer_url: 'http://nttsbreakdown.com/ntts/programs/main/main.php',
                 searchBy: 'citystate',
                 visitor_agent: 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0',
                 visitor_browser_height: '350',
                 visitor_browser_width: '1536',
                 visitor_color_depths: '24',
                 visitor_cookies: '1',
                 visitor_java: '1',
                 visitor_language: 'en-US',
                 visitor_resolution_height: '864',
                 visitor_resolution_width: '1536',
                 visitor_timezone_offset: '-420'
             },
             success: function(data) {
                 crawler.saveTrucks(countryCode, stateId, cityId, cateId, data);
             }
         });
     },

     saveTrucks : function(countryCode, stateId, cityId, cateId, trucks) {
         // push data to server and save
         // when save comletely then call updateCityState to mark crawled cities as completed
     },
     
     updateCityState : function() {
         // set city state is crawled successfully 
     },
     
     updateStateState : function() {
         // set state state is crawled successfully 
     },
     
     crawlTrucks : function(countryCode, stateId, cityId) {
         // loop cities and download trucks (call getTrucks function)
     },
     
     getUncraledCities : function() {
         // loop states
         // get a city from local server that have not crawled yet
         // call crawlTrucks(countryCode, stateId, cityId)
         // when finish crawling all cities then update the State as completed
         // call updateStateState
     },
     
};
